/**
 * 
 * @param second 计时秒数
 * @param duraction 计时间隔
 */
type Ops = {
	startSecond: number,
	targetSecond: number,
	duraction: number,
	type: 'countDown' | 'countUp',
	triggerTarget ?: () => void,
};
export function useTimer(ops: Ops) {
	const timerId = ref(0);
	const currentSecond = ref(ops.startSecond);
	const start = () => {
		if (timerId.value) return;
		timerId.value = setInterval(() => {
			if (ops.type === 'countDown') {
				currentSecond.value--;
				if (currentSecond.value <= 0) {
					ops.triggerTarget && ops.triggerTarget();
					stop();
				};
			};
			if (ops.type === 'countUp') {
				currentSecond.value++;
				if (currentSecond.value >= ops.targetSecond) {
					ops.triggerTarget && ops.triggerTarget();
					stop();
				};
			};
		}, ops.duraction * 1000);
	};

	const stop = () => {
		timerId.value && clearInterval(timerId.value);
		timerId.value = 0;
		currentSecond.value = ops.startSecond;
	};

	const setTime = (t: number) => {
		currentSecond.value = t;
	};

	return {
		timerId,
		currentSecond,
		start,
		stop,
		setTime
	};
};